<template>
    <div class="comment-list" v-for="item in commentListValue" :key="item.id">
        <div class="comment-root" v-if="item">
            <div class="avatar">
                <img :src="item.avatar">
            </div>
            <div class="right">
                <div class="header">
                    <div class="user">
                        {{ item.userNickname }}
                    </div>
                    <div class="time">
                        {{ item.createTime }}
                    </div>
                </div>
                <div class="content">
                    <p>{{ item.content }}</p>
                </div>
                <div class="footer">
                    <span @click="handleDelete(item.id)" v-if="item.createBy == userInfo.id"
                        style="color: red;">删除</span>
                    <span style="color: aqua;" @click="handleClick(item.id)">回复</span>
                </div>
                <div>
                    <reply @cancelComment="cancelComment" v-if="createId == item.id" :commentInfo="item">
                    </reply>
                </div>
                <div class="comment-child" v-if="item.children" v-for="ite in item.children" :key="ite.id">
                    <div class="avatar">
                        <img :src="ite.avatar">
                    </div>
                    <div class="right">
                        <div class="header">
                            <div class="user">
                                {{ ite.userNickname }}
                            </div>
                            <div class="time">
                                {{ ite.createTime }}
                            </div>
                        </div>
                        <div class="content">
                            <div style="font-weight: bolder;">
                                <span>回复</span><span style="margin-left: .1rem; color: red;">@{{ ite.toCommentNickname
                                    }}</span><span>:</span>
                            </div>
                            <p>{{ ite.content }}</p>
                        </div>
                        <div class="footer">
                            <span @click="handleDelete(ite.id)" v-if="ite.createBy == userInfo.id"
                                style="color: red;">删除</span>
                            <span style="color: aqua;" @click="handleClick(ite.id)">回复</span>
                        </div>
                        <div>
                            <reply @cancelComment="cancelComment" v-if="createId == ite.id" :commentInfo="ite">
                            </reply>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>


<script setup>
import { useUserInfoStore } from '@/store/userInfo/index.js';
import reply from './reply.vue';
import { ref } from 'vue';
const props = defineProps({
    commentListValue: {
        type: Object,
        default: {}
    }
})
const userInfo = useUserInfoStore().userInfo

const createId = ref(-1)

const handleClick = (val) => {
    createId.value = val
}
const emits = defineEmits(['deleteComment'])

const handleDelete = (val) => {
    emits('deleteComment', val)
}

const cancelComment = (val) => {
    createId.value = val
}
</script>


<style lang="scss" scoped>
.comment-list {
    margin-top: 2rem;
    border-top: .1rem solid #777;
}

.comment-root {
    display: flex;
    padding: 2rem 0;

}

.comment-root .avatar {
    height: 5rem;
    width: 5rem;
    border-radius: 1rem;
    border: none;

    img {
        height: 100%;
        width: 100%;
        border-radius: 1rem;
    }
}

.comment-root .right {
    flex: 1 1 1rem;

    .footer {
        display: flex;
        justify-content: end;

        span {
            cursor: pointer;
            margin: 0 1rem;
            font-size: 1.4rem;
            font-family: 'SimSun';
        }
    }
}

.comment-root .right .header {
    margin: 0 1rem;
    display: flex;

    div {
        display: flex;
        align-items: center;
    }

    .user {
        font-size: 1.8rem;
        font-weight: bolder;
    }

    .time {
        font-size: 1.4rem;
        margin: 0 2rem;
    }
}

.comment-root .content {
    padding: 1.2rem;
    background: #f2f2f2;
    border-radius: 1rem;
    font-size: 1.4rem;
    align-items: center;
    box-sizing: border-box;
    line-height: 2;
    margin: .6rem 1rem;
}

.comment-child {
    border-top: .1rem solid #777;
    margin-top: 1.4rem;
    display: flex;
    padding: 2rem 0;
}
</style>